/**
 * 
 */
package com.wilmar.process.web.actions;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.wilmar.common.comparator.CommonDescendingComparator;
import com.wilmar.common.util.PageinationEntity;
import com.wilmar.process.core.ProcessSystemDeclareKeys;
import com.wilmar.process.core.services.api.ISystemHistoryTaskInstanceService;
import com.wilmar.process.core.services.api.ISystemTaskInstanceService;
import com.wilmar.process.entity.InstanceOperateOpinion;
import com.wilmar.process.entity.User;
import com.wilmar.process.entity.query.HistoryTaskInstanceQueryObject;
import com.wilmar.process.entity.query.ProcessTakenTaskQueryObject;
import com.wilmar.process.entity.query.ProcessUnassignTaskQueryObject;
import com.wilmar.process.entity.vo.HistoryTaskInstanceVO;
import com.wilmar.process.entity.vo.TaskInstanceVO;
import com.wilmar.process.enumerate.EnumProcessInstanceState;
import com.wilmar.process.enumerate.EnumProcessSystemTagFixedVariableNameSet;
import com.wilmar.process.exception.InvalidTaskInstanceException;
import com.wilmar.process.exception.NoUserSessionException;
import com.wilmar.process.exception.ObjectNotFoundException;
import com.wilmar.process.exception.PermissionDeniedException;
import com.wilmar.process.exception.TaskAlreadyProcessedException;
import com.wilmar.process.exception.TaskAlreadyTakenByAnotherUserException;
import com.wilmar.process.exception.TaskRepeatAcceptedException;
import com.wilmar.process.web.form.TaskInstanceCommentForm;

/**
 * @author MaKuBEX E-mail:makubexnet@gmail.com
 * @version 1.1.4 at Aug 3, 2009 1:47:21 PM
 * 
 */
public class TaskInstanceAction extends ProcessActionSupport {

	protected static final String ACTION_RESULT_UNASSIGNTASKLIST = "unassign-tasklist";
	protected static final String ACTION_RESULT_TAKENTASKLIST = "taken-tasklist";
	protected static final String ACTION_RESULT_TAKENTASKLIST_ACTION = "taken-tasklist-action";
	protected static final String ACTION_RESULT_UNASSIGNTASKLIST_ACTION = "unassign-tasklist-action";
	protected static final String ACTION_RESULT_HISTORYTASKLIST = "history-tasklist";
	protected static final String ACTION_RESULT_TASKFORM = "task-form";
	protected static final String ACTION_RESULT_TASKDETAIL_ACTION = "task-detail-action";
	protected static final String ACTION_RESULT_HISTORYTAKS_SEARCH = "historytask-search";
	protected static final String ACTION_RESULT_UNASSIGNTASK_SEARCH = "unassign-task-search";
	protected static final String ACTION_RESULT_TAKENTASK_SEARCH = "taken-task-search";
	
	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 2600660022792937214L;

	protected ISystemTaskInstanceService systemTaskInstanceService;
	protected ISystemHistoryTaskInstanceService systemHistoryTaskInstanceService;

	protected List<HistoryTaskInstanceVO> historyTaskInstanceVoList;
	protected List<TaskInstanceVO> taskInstanceVOList;
	protected String currentTaskForm;

	protected TaskInstanceCommentForm taskInstanceCommentForm;

	protected ProcessUnassignTaskQueryObject processUnassignTaskQueryObject;
	protected ProcessTakenTaskQueryObject processTakenTaskQueryObject;

	protected Map<String, String> processInstanceStateSelection; // 流程状态
	protected HistoryTaskInstanceQueryObject historyTaskInstanceQueryObject; // 查询实体封装

	public String doRevokeTask() throws ObjectNotFoundException,
			PermissionDeniedException, NoUserSessionException {
		User revoker = super.getCurrentUser();
		super.processInformation
				.setTaskInstanceId(this.systemHistoryTaskInstanceService
						.revokeTask(super.processInformation
								.getTaskInstanceId(), revoker));
		this.currentTaskForm = this.systemTaskInstanceService
				.getTaskForm(super.processInformation.getTaskInstanceId());
		Map<String, ?> variableMap = this.systemTaskInstanceService
				.getDataByTaskInstanceId(super.processInformation
						.getTaskInstanceId());
		Iterator<String> iter = variableMap.keySet().iterator();
		while (iter.hasNext()) {
			String paraName = iter.next();
			super.request.setAttribute(paraName, variableMap.get(paraName));
		}
		super.request
				.setAttribute(
						EnumProcessSystemTagFixedVariableNameSet.PROCESS_INFORMATION_TASK_INSTANCE_ID
								.getValue(), super.processInformation
								.getTaskInstanceId());
		return ACTION_RESULT_TASKFORM;
	}

	public String doAddComment() throws PermissionDeniedException,
			NoUserSessionException {
		User currentUser = super.getCurrentUser();
		this.systemTaskInstanceService.addComment(this.taskInstanceCommentForm
				.getTaskInstanceId(),
				this.taskInstanceCommentForm.getComment(), currentUser, false);
		return ACTION_RESULT_TASKDETAIL_ACTION;
	}

	public String doReplyComment() throws NoUserSessionException {
		User currentUser = super.getCurrentUser();
		this.systemTaskInstanceService.addCommentReply(
				this.taskInstanceCommentForm.getTaskInstanceId(),
				this.taskInstanceCommentForm.getCommentId(),
				this.taskInstanceCommentForm.getReply(), currentUser);
		return ACTION_RESULT_TASKDETAIL_ACTION;
	}

	public String doCompleteTask() throws PermissionDeniedException,
			NoUserSessionException {
		User user = super.getCurrentUser();
		InstanceOperateOpinion opinion = new InstanceOperateOpinion(
				super.processInformation.getOpinion());
		this.systemTaskInstanceService.completeTask(super.processInformation
				.getTaskInstanceId(), super.processInformation.getOutCome(),
				user, opinion, super.request.getParameterMap(), false);
		return ACTION_RESULT_TAKENTASKLIST_ACTION;
	}

	public String doGetReceivedUserTaskListQueryObject()
			throws NoUserSessionException {
		User user = super.getCurrentUser();
		this.taskInstanceVOList = this.systemTaskInstanceService
				.convertToVOList(this.systemTaskInstanceService
						.getListTakenTaskInstanceByUser(user,
								this.processTakenTaskQueryObject));
		Collections.sort(this.taskInstanceVOList,
				new CommonDescendingComparator());

		return ACTION_RESULT_TAKENTASKLIST;
	}

	public String doGetTaskDetail() throws InvalidTaskInstanceException {
		try {
			this.currentTaskForm = this.systemTaskInstanceService
					.getTaskForm(super.processInformation.getTaskInstanceId());
		} catch (ObjectNotFoundException e) {
			throw new InvalidTaskInstanceException(e.getMessage());
		}
		Map<String, ?> variableMap = this.systemTaskInstanceService
				.getDataByTaskInstanceId(super.processInformation
						.getTaskInstanceId());
		Iterator<String> iter = variableMap.keySet().iterator();
		while (iter.hasNext()) {
			String paraName = iter.next();
			super.request.setAttribute(paraName, variableMap.get(paraName));
		}
		return ACTION_RESULT_TASKFORM;
	}

	public String doGetUnassignedUserTaskListQueryObject()
			throws NoUserSessionException {
		User user = super.getCurrentUser();
		this.taskInstanceVOList = this.systemTaskInstanceService
				.convertToVOList(this.systemTaskInstanceService
						.getListAllUnAssignedTaskInstanceByUser(user,
								this.processUnassignTaskQueryObject));
		Collections.sort(this.taskInstanceVOList,
				new CommonDescendingComparator());
		return ACTION_RESULT_UNASSIGNTASKLIST;
	}

	public String doPopTask() throws PermissionDeniedException,
			NoUserSessionException {
		User user = super.getCurrentUser();
		this.systemTaskInstanceService.popTask(super.processInformation
				.getTaskInstanceId(), user);
		return ACTION_RESULT_TAKENTASKLIST_ACTION;
	}

	public String doGetTaskHistroyList() throws ObjectNotFoundException,
			NoUserSessionException {
		User user = super.getCurrentUser(); // 当前用户
		if (super.pageination == null)
			super.pageination = new PageinationEntity(0,
					ProcessSystemDeclareKeys.SYSTEM_DEFAULT_PAGEINATION_LIMIT); // 分页信息
		if (this.historyTaskInstanceQueryObject != null) {
			this.historyTaskInstanceQueryObject
					.setPageination(super.pageination);
		}
		this.historyTaskInstanceVoList = this.systemHistoryTaskInstanceService
				.getVOListHistroyTaskList(user,
						this.historyTaskInstanceQueryObject);
		return ACTION_RESULT_HISTORYTASKLIST;
	}

	public String doSearchHistoryTaskForward() {
		this.processInstanceStateSelection = new HashMap<String, String>();
		for (EnumProcessInstanceState state : EnumProcessInstanceState.values()) {
			this.processInstanceStateSelection.put(state.name(), state.name());
		}
		return ACTION_RESULT_HISTORYTAKS_SEARCH;
	}

	public String doTakeTask() throws PermissionDeniedException,
			TaskAlreadyTakenByAnotherUserException,
			InvalidTaskInstanceException, TaskAlreadyProcessedException,
			ObjectNotFoundException, NoUserSessionException {
		User user = super.getCurrentUser();
		try {
			this.systemTaskInstanceService.takeTask(super.processInformation
					.getTaskInstanceId(), user);
		} catch (TaskRepeatAcceptedException ex) {
			return this.doGetTaskDetail();
		}
		return ACTION_RESULT_TASKDETAIL_ACTION;
	}

	public String doUnassignTaskSearchForward() {

		return ACTION_RESULT_UNASSIGNTASK_SEARCH;
	}

	public String doTakenTaskSearchForward() {

		return ACTION_RESULT_TAKENTASK_SEARCH;
	}

	/**
	 * @return the systemTaskInstanceService
	 */
	public ISystemTaskInstanceService getSystemTaskInstanceService() {
		return systemTaskInstanceService;
	}

	/**
	 * @param systemTaskInstanceService
	 *            the systemTaskInstanceService to set
	 */
	public void setSystemTaskInstanceService(
			ISystemTaskInstanceService systemTaskInstanceService) {
		this.systemTaskInstanceService = systemTaskInstanceService;
	}

	/**
	 * @return the currentTaskForm
	 */
	public String getCurrentTaskForm() {
		return currentTaskForm;
	}

	/**
	 * @param currentTaskForm
	 *            the currentTaskForm to set
	 */
	public void setCurrentTaskForm(String currentTaskForm) {
		this.currentTaskForm = currentTaskForm;
	}

	/**
	 * @return the taskInstanceVOList
	 */
	public List<TaskInstanceVO> getTaskInstanceVOList() {
		return taskInstanceVOList;
	}

	/**
	 * @param taskInstanceVOList
	 *            the taskInstanceVOList to set
	 */
	public void setTaskInstanceVOList(List<TaskInstanceVO> taskInstanceVOList) {
		this.taskInstanceVOList = taskInstanceVOList;
	}

	public List<HistoryTaskInstanceVO> getHistoryTaskInstanceVoList() {
		return historyTaskInstanceVoList;
	}

	public void setHistoryTaskInstanceVoList(
			List<HistoryTaskInstanceVO> historyTaskInstanceVoList) {
		this.historyTaskInstanceVoList = historyTaskInstanceVoList;
	}

	public Map<String, String> getProcessInstanceStateSelection() {
		return processInstanceStateSelection;
	}

	public void setProcessInstanceStateSelection(
			Map<String, String> processInstanceStateSelection) {
		this.processInstanceStateSelection = processInstanceStateSelection;
	}

	/**
	 * @return the historyTaskInstanceQueryObject
	 */
	public HistoryTaskInstanceQueryObject getHistoryTaskInstanceQueryObject() {
		return historyTaskInstanceQueryObject;
	}

	/**
	 * @param historyTaskInstanceQueryObject
	 *            the historyTaskInstanceQueryObject to set
	 */
	public void setHistoryTaskInstanceQueryObject(
			HistoryTaskInstanceQueryObject historyTaskInstanceQueryObject) {
		this.historyTaskInstanceQueryObject = historyTaskInstanceQueryObject;
	}

	public ProcessUnassignTaskQueryObject getProcessUnassignTaskQueryObject() {
		return processUnassignTaskQueryObject;
	}

	public void setProcessUnassignTaskQueryObject(
			ProcessUnassignTaskQueryObject processUnassignTaskQueryObject) {
		this.processUnassignTaskQueryObject = processUnassignTaskQueryObject;
	}

	public ProcessTakenTaskQueryObject getProcessTakenTaskQueryObject() {
		return processTakenTaskQueryObject;
	}

	public void setProcessTakenTaskQueryObject(
			ProcessTakenTaskQueryObject processTakenTaskQueryObject) {
		this.processTakenTaskQueryObject = processTakenTaskQueryObject;
	}

	/**
	 * @return the taskInstanceCommentForm
	 */
	public TaskInstanceCommentForm getTaskInstanceCommentForm() {
		return taskInstanceCommentForm;
	}

	/**
	 * @param taskInstanceCommentForm
	 *            the taskInstanceCommentForm to set
	 */
	public void setTaskInstanceCommentForm(
			TaskInstanceCommentForm taskInstanceCommentForm) {
		this.taskInstanceCommentForm = taskInstanceCommentForm;
	}

	/**
	 * @return the systemHistoryTaskInstanceService
	 */
	public ISystemHistoryTaskInstanceService getSystemHistoryTaskInstanceService() {
		return systemHistoryTaskInstanceService;
	}

	/**
	 * @param systemHistoryTaskInstanceService
	 *            the systemHistoryTaskInstanceService to set
	 */
	public void setSystemHistoryTaskInstanceService(
			ISystemHistoryTaskInstanceService systemHistoryTaskInstanceService) {
		this.systemHistoryTaskInstanceService = systemHistoryTaskInstanceService;
	}

}
